package basic.util;
import java.math.BigDecimal;
import java.math.BigInteger;


public class FactorialTest {
	public static void main(String[] args) {
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Long.MAX_VALUE);
		int i=0;
		for(i=1;i<89;i++){
			long product = factorial(i);
			if(product<0){
				i--;
				break;
			}
			System.out.println(product);
		}
		System.out.println("i="+i);
		
		for(i=1;i<89;i++){
			BigInteger product = factorialBigInteger(i);
			System.out.println(product);
			if(product.testBit(64))
				break;
		}
		System.out.println("i="+i);
		
		for(i=1;i<89;i++){
			BigDecimal product = factorialBigDecimal(i);
			System.out.println(product);
			if(product.compareTo(new BigDecimal(Long.MAX_VALUE))>0)
				break;
		}
		System.out.println("i="+i);
	}
	
	public static long factorial(long n){
		if(n<=1)
			return 1;
		long product = 1;
		for(int i=2;i<=n;i++)
			product*=i;
		return product;
	}
	
	public static BigDecimal factorialBigDecimal(long n){
		if(n<=1)
			return new BigDecimal(1);
		BigDecimal product = new BigDecimal(1);
		for(int i=2;i<=n;i++)
			product = product.multiply(new BigDecimal(i));
		return product;
	}
	
	public static BigInteger factorialBigInteger(long n){
		if(n<=1)
			return BigInteger.valueOf(1);
		BigInteger product = BigInteger.valueOf(1);
		for(int i=2;i<=n;i++)
			product = product.multiply(BigInteger.valueOf(i));
		return product;
	}
}
